#include "socket_helper.h"
#include "constant.h"
#include <QObject>
#include <functional>
#include "util.h"
#include "table_item.h"

using namespace dymy;
SocketHelper::SocketHelper(QWebSocket *w): websocket(w)
{

}

SocketHelper::~SocketHelper()
{

}

void SocketHelper::_send_socket_message(const int &code, const QJsonObject &data) {
    send_socket_message(websocket,code,data);
}

TableHelper::TableHelper(QWebSocket *w):SocketHelper(w)
{

}


void TableHelper::on_connected()
{

}

void TableHelper::on_message(const QByteArray &message)
{

}


void TableHelper::on_message(const QString &message)
{
    handle_server_result(message.toUtf8(),[&](const int &code,const QJsonDocument &data,const QString &tag){
        if(code==REP_TYPE_SERVER_INFO)
        {
            QVector<TableItem> data_bean;

            QJsonArray arr =  data.array();
            QJsonArray::iterator begin =  arr.begin();
            QJsonArray::iterator end = arr.end();

            while(begin!=end)
            {
                QJsonObject json_table =  (*begin++).toObject();
                TableItem itemData;
                TableItem::json2data(json_table,itemData);
                data_bean.append(itemData);
            }

            on_server_info_message(data_bean);
        }
        else if(code ==REP_TYPE_CHAT)
        {
            ChatData data_bean;
            ChatData::json2data(data.object(),data_bean);
            on_chat_message(data_bean);
        }
        else if(code ==REP_TYPE_PERSON_CHANGED_NOTIFY)
        {
            PersonNotifyhData data_bean;
            PersonNotifyhData::json2data(data.object(),data_bean);
            on_person_changed_notify(data_bean);
        }


    },
    [&](const int &code,const int &rep_code,const QString &rep_message,const QString &tag){
        if(code==REP_TYPE_SERVER_INFO)
        {
            on_table_response_error(code,rep_code,rep_message,tag);
        }

        else if(code ==REP_TYPE_CHAT)
        {
            on_table_response_error(code,rep_code,rep_message,tag);
        }
        else if(code ==REP_TYPE_PERSON_CHANGED_NOTIFY)
        {
            on_table_response_error(code,rep_code,rep_message,tag);
        }
    }
    );
}



void TableHelper::on_disconnect()
{

}

void TableHelper::_send_chat_message(const QString &message)
{
    QJsonObject data;
//    data.insert("user_id",user_data.id);
    QJsonObject content;
    content.insert("message",message);
    data.insert("content",content);
    _send_socket_message(REQ_TYPE_CHAT,data);
}

void TableHelper::_send_server_info_message(bool notify)
{
    QJsonObject data;
//    data.insert("user_id",user_data.id);
    data.insert("notify",notify);//后面会陆续是收到牌桌人员进出通知
    _send_socket_message(REQ_TYPE_SERVER_INFO,data);
}

void GameHelper::on_connected()
{

}

void GameHelper::on_message(const QByteArray &message)
{

}

void GameHelper::on_message(const QString &message)
{
    handle_server_result(message.toUtf8(),[&](const int &code,const QJsonDocument &data,const QString &tag){
        if(code==REP_TYPE_ADD_PERSON)
        {
            TabaleData data_bean;
            TabaleData::json2data(data.object(),data_bean);
            on_add_table_message(data_bean);
        }
        else if(code==REP_TYPE_PREPARED)
        {
            TabaleData data_bean;
            TabaleData::json2data(data.object(),data_bean);
            on_prepare_table_message(data_bean);

        }
        else if(code==REP_TYPE_ROB_LANDLORD)
        {
            TabaleData data_bean;
            TabaleData::json2data(data.object(),data_bean);
            on_rob_table_message(data_bean);

        }else if(code==REP_TYPE_PLAY_CARDS)
        {
            TabaleData data_bean;
            TabaleData::json2data(data.object(),data_bean);
            on_cards_table_message(data_bean);
        }
        else if(code ==REP_TYPE_CHAT_TABLE)
        {
            ChatTableData data_bean;
            ChatTableData::json2data(data.object(),data_bean);
            on_chat_table_message(data_bean);
        }
    },
    [&](const int &code,const int &rep_code,const QString &rep_message,const QString &tag){
        if(code==REP_TYPE_ADD_PERSON)
        {
            on_game_response_error(code,rep_code,rep_message,tag);
        }
        else if(code==REP_TYPE_PREPARED)
        {
            on_game_response_error(code,rep_code,rep_message,tag);
        }
        else if(code==REP_TYPE_ROB_LANDLORD)
        {
            on_game_response_error(code,rep_code,rep_message,tag);
        }else if(code==REP_TYPE_PLAY_CARDS)
        {
            on_game_response_error(code,rep_code,rep_message,tag);
        }
        else if(code ==REP_TYPE_CHAT_TABLE)
        {
            on_game_response_error(code,rep_code,rep_message,tag);
        }
    }
    );
}

void GameHelper::on_disconnect()
{

}

GameHelper::GameHelper(QWebSocket *w,  int table_id, int index):SocketHelper(w),table_id(table_id),table_index(index)
{

}

void GameHelper::_send_join_table_message(int id, int index, int action)
{
    QJsonObject data;
    data.insert("id",id);
    data.insert("index",index);
    data.insert("action",action);
    _send_socket_message(REQ_TYPE_ADD_PERSON,data);
}

void GameHelper::_send_prepare_table_message(int id, int index, int action)
{
    QJsonObject data;
    data.insert("id",id);
    data.insert("index",index);
    data.insert("action",action);
    _send_socket_message(REQ_TYPE_PREPARED,data);
}

void GameHelper::_send_chat_table_message(const QString &message)
{
    QJsonObject data;
    data.insert("table_id",table_id);
    QJsonObject content;
    content.insert("message",message);
    data.insert("content",content);
    _send_socket_message(REP_TYPE_CHAT_TABLE,data);
}

void GameHelper::_send_rob_table_message(int id, int index, int action)
{
    QJsonObject data;
    data.insert("id",id);
    data.insert("index",index);
    data.insert("action",action);
    _send_socket_message(REQ_TYPE_ROB_LANDLORD,data);
}

void GameHelper::_send_cards_table_message(int id, int index, const QVector<dymy::CARD_t> &cards)
{
    QJsonObject data;
    data.insert("id",id);
    data.insert("index",index);

    QJsonArray j_arr;
    for(const CARD_t &c:cards)
    {
        j_arr.append(c);
    }
    data.insert("cards",j_arr);
    _send_socket_message(REQ_TYPE_PLAY_CARDS,data);
}
